package com.ruoyi.milvus.contoller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.milvus.domain.Face;
import com.ruoyi.milvus.mapper.FaceMilvusMapper;
import io.milvus.v2.service.vector.response.InsertResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.milvus.plus.model.vo.MilvusResp;
import org.dromara.milvus.plus.model.vo.MilvusResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/milvus/face")
@RequiredArgsConstructor
@Slf4j
public class FaceController {
    private final FaceMilvusMapper mapper;

    @GetMapping("/insert")
    public AjaxResult insert() {
        //新增
        Face face1 = new Face();
        face1.setPersonId(1l);
        List<Float> vector = new ArrayList<>();
        for (int i = 0; i < 128; i++) {
            vector.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数
        }
        face1.setFaceVector(vector);
        MilvusResp<InsertResp> insert = mapper.insert(face1);
        log.info("insert--{}", JSONObject.toJSONString(insert));
        return AjaxResult.success();
    }

    @GetMapping("/insertBatch")
    public AjaxResult insertBatch() {
        //新增
        List<Face> faces = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            Face face1 = new Face();
            face1.setPersonId(Long.valueOf(i));
            List<Float> vector1 = new ArrayList<>();
            for (int j = 0; j < 128; j++) {
                vector1.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数
            }
            face1.setFaceVector(vector1);
            faces.add(face1);
        }
        MilvusResp<InsertResp> insert = mapper.insert(faces.toArray(faces.toArray(new Face[0])));
        log.info("insert--{}", JSONObject.toJSONString(insert));
        return AjaxResult.success();
    }

    @GetMapping("/query")
    public AjaxResult query() {
        //id查询
        MilvusResp<List<MilvusResult<Face>>> query = mapper.getById(9l);
        log.info("query--getById---{}", JSONObject.toJSONString(query));
        return AjaxResult.success(JSONObject.toJSONString(query));
    }

    @GetMapping("/queryVector")
    public AjaxResult queryVector() {
        List<Float> vector = new ArrayList<>();
        for (int i = 0; i < 128; i++) {
            vector.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数
        }
        //向量查询
        MilvusResp<List<MilvusResult<Face>>> query1 = mapper.queryWrapper()
                .vector(Face::getFaceVector, vector)
                .ne(Face::getPersonId, 1L)
                .topK(3)
                .query();
        log.info("向量查询 query--queryWrapper---{}", JSONObject.toJSONString(query1));
        //标量查询
        MilvusResp<List<MilvusResult<Face>>> query2 = mapper.queryWrapper()
                .eq(Face::getPersonId, 2L)
                .limit(3L)
                .query();
        log.info("标量查询   query--queryWrapper---{}", JSONObject.toJSONString(query2));
        return AjaxResult.success(JSONObject.toJSONString(query2));
    }

}
